package com.tn.rtdata.management;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tn.rtdata.DataEventStats;

/**
 * A manager service that manages data-event stats.
 */
public class DataEventsStatsManager
{
  private static final Logger LOGGER = LoggerFactory.getLogger(DataEventsStatsManager.class);

  private Map<String, DataEventStats> dataEventStatsCache;

  /**
   * Creates a new <code>DataEventsStatsManager</code> initialized with the <code>dataEventStatsCache</code>.
   */
  public DataEventsStatsManager(Map<String, DataEventStats> dataEventStatsCache)
  {
    this.dataEventStatsCache = dataEventStatsCache;
  }

  /**
   * Updates that statistics for the <code>dataEventId</code> in response to a client having processed a <code>DataEvent</code>.
   */
  public void dataEventProcessed(String dataEventId)
  {
    DataEventStats dataEventStats = dataEventStatsCache.get(dataEventId);

    if (dataEventStats == null)
    {
      LOGGER.info("No data event stats found in cache for data event: {}", dataEventId);
      return;
    }

    dataEventStats = dataEventStats.processed(new Date());
    dataEventStatsCache.put(dataEventId, dataEventStats);

    LOGGER.debug("Data event stats updated: {}", dataEventStats);
  }
}
